<template>
	<view class="py-2">
		<template v-if="item.status===1">
			<view class="flex align-center justify-end">
				<view class="flex align-center justify-center cancle"  @click.stop="cancleClick">取消订单</view>
				<view class="flex align-center justify-center pay ml-2"  @click.stop="show=true">去付款</view>
			</view>
		</template>
		<!-- 订单详情时显示 -->
		<template v-if="item.status===2">
			<view class="flex align-center justify-end">
				<view class="flex align-center justify-center cancle"  @click.stop="afterClick">申请售后</view>
			</view>
		</template>
		<!-- 订单详情是多显示申请售后与查看物流 -->
		<template v-if="item.status===3">
			<view class="flex align-center justify-end">
				<view class="flex align-center justify-center cancle"  @click.stop="afterClick">申请售后</view>
				<view class="flex align-center justify-center cancle ml-2"  @click.stop="checkLogistics">查看物流</view>
				<view class="flex align-center justify-center pay ml-2"  @click.stop="shouClick">确认收货</view>	
			</view>
		</template>
		<!-- 订单详情是多显示删除订单 -->
		<template v-if="item.status===4">
			<view class="flex align-center justify-end">
				<view class="flex align-center justify-center cancle"  @click.stop="godelete">删除订单</view>
				<view class="flex align-center justify-center pay ml-2"  @click.stop="gocomment">去评价</view>
			</view>
		</template>
		<template v-if="item.status===7||item.status===5">
			<view class="flex align-center justify-end">
				<view class="flex align-center justify-center cancle"  @click.stop="godelete">删除订单</view>	
			</view>
		</template>
		
		<u-popup :show="show">
			<commonPay :curIndex="curIndex" type="order" @click="changepay" @pay="payClick" @close="show=false"></commonPay>
		</u-popup>
		
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default{
		props: {
			item: Object,
			type: String
		},
		data(){
		  return {
			  curIndex: 0,
			  show:false
		  }	
		},
		methods:{
			...mapMutations(['getDetail']),
			afterClick(){
				this.getDetail(this.item)
				// console.log(this.item.goods_order)
				uni.$u.route('pages/user/aftersale/apply',this.item.goods_order)
			},
			cancleClick() {
				console.log(this.item)
				uni.showModal({
					title: '提示',
					content: '确定取消此订单吗',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if(res.confirm){
							this.$api.cancelOrder({
								order_id:this.item.id
							}).then(res=>{
								if(res.code === 1){
									uni.showToast({
										title: '取消成功',
										icon: 'success'
									});
									this.$emit('refresh')
								}
							})
						}
					},
					
				});	
			},
			shouClick() {
				uni.showModal({
					title: '提示',
					content: '请确定是否已收到货',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if(res.confirm){
							this.$api.confirmOrder({
								order_id:this.item.id
							}).then(res=>{
								if(res.code === 1){
									uni.showToast({
										title: '收货成功',
										icon: 'success'
									});
									this.$emit('refresh')
								}
							})
						}
					},
					
				});	
			},
			changepay(index) {
				this.curIndex = index
			},
			payClick(){
				this.show = false
				if(uni.$u.platform === 'mp'){
					let parmar = {
						order_id:this.item.id,
						pay_way: this.curIndex === 0 ? 1 :  3,
					}
					this.$api.wxpay(parmar).then(res=>{
						if(res.code === 1){
							if(this.curIndex === 0){
								uni.requestPayment({
									provider:'wxpay',
									timeStamp:res.data.timeStamp,
									nonceStr:res.data.nonceStr,
									package:res.data.package,
									signType:res.data.signType,
									paySign:res.data.paySign,
									success: (res) => {
										this.$emit('refresh')
									}
								})
							}else{
								
								uni.showToast({
									title: '支付成功',
									icon:'none',
								});
								setTimeout(()=>{
									this.$emit('refresh')
								},500)
							}
						}
						
					})
				}else{
					let pay_way = this.curIndex === 0?2:1
					let parmar = {
						order_id:this.item.id,
						pay_way:pay_way
					}
					this.$api.orderpay(parmar).then(res=>{
						if(res.code === 1){
							console.log(res)
							console.log(parmar)
							uni.requestPayment({
								provider:this.curIndex===0?'alipay':'wxpay',
								orderInfo: res.data,
								success: (result) => {
									uni.showToast({
										title: '支付成功',
										icon:'none',
									});
									this.$emit('refresh')
								},
								fail: (err) => {
									
								}
							})
							
						}
					})
					
				}
				
			},
			gocomment(){
				this.getDetail(this.item)
				uni.$u.route('pages/user/evaluate')
			},
			godelete(){
				uni.showModal({
					title: '提示',
					content: '确定删除此订单吗',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if(res.confirm){
							this.$api.delCancel({
								order_id:this.item.id
							}).then(res=>{
								if(res.code === 1){
									uni.showToast({
										title: '删除成功',
										icon: 'success'
									});
									this.$emit('refresh')
								}
							})
						}
					},
					
				});	
				
			},
			// checklogistics(){
			// 	uni.$u.route('pages/user-logistics/user-logistics',{
			// 		orderId:this.item.id
			// 	})
			// },
			// 查看物流
			checkLogistics() {
				uni.$u.route('pages/user/logistics',{
					orderId: this.item.id
				})
			}
			
		}
	}
</script>

<style>
	.cancle{
		line-height: 56rpx;
		width: 156rpx;
		height: 56rpx;
		background: #fff;
		border-radius: 202rpx 202rpx 202rpx 202rpx;
		opacity: 1;
		border: 2rpx solid #dbdbdb;
		font-size: 24rpx;
		font-family: Source Han Sans CN-Regular, Source Han Sans CN;
		font-weight: 400;
		color: #5f5f5f;
	}
	.pay{
		line-height: 56rpx;
		width: 156rpx;
		height: 56rpx;
		background: #85b983;
		border-radius: 202rpx 202rpx 202rpx 202rpx;
		opacity: 1;
		font-size: 24rpx;
		font-family: Source Han Sans CN-Regular, Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
	}
</style>